﻿using System;
using System.Collections.Generic;
using System.Web.Routing;
using Orchard;
using Orchard.UI.Navigation;

namespace dsendelb.Navigation.Services
{
    public class MenuItemService : IMenuItemService {
        private readonly IWorkContextAccessor _workContextAccessor;

        public MenuItemService(IWorkContextAccessor accessor) {
            _workContextAccessor = accessor;
        }
        public Stack<MenuItem> SetSelectedPath(IEnumerable<MenuItem> menuItems, RouteData currentRouteData) {
            var selectedPath = NavigationHelper.SetSelectedPath(menuItems, currentRouteData);
          
            //add active class to each item
            if (selectedPath != null) {
                foreach (var menuItem in selectedPath) {
                    if (!menuItem.Classes.Contains("active"))
                    {
                        menuItem.Classes.Add("active");
                    }
                }
            }
            else {
                //if the core method didn't return a result try using the URL in our private method instead.
                selectedPath = TryAddItems(menuItems);
            }
            return selectedPath;
        }


        private Stack<MenuItem> TryAddItems(IEnumerable<MenuItem> menuItems) {
            var result = new Stack<MenuItem>();
            var currentRequest = _workContextAccessor.GetContext().HttpContext.Request;
            if (currentRequest != null && !String.IsNullOrWhiteSpace(currentRequest.ApplicationPath))
            {
                string requestUrl = currentRequest.Path.Replace(currentRequest.ApplicationPath, string.Empty).TrimEnd('/').ToUpperInvariant();
                result = HighlightPath(menuItems, requestUrl, currentRequest.ApplicationPath);  
            }

            return result;
        }

        //this recursive method highlights the active trail based on URL
        private static Stack<MenuItem> HighlightPath(IEnumerable<MenuItem> menuItems, string requestUrl, string appPath)
        {
            if (menuItems == null)
                return null;

            foreach (MenuItem menuItem in menuItems)
            {
                Stack<MenuItem> selectedPath = HighlightPath(menuItem.Items, requestUrl, appPath);
                if (selectedPath != null)
                {
                    menuItem.Selected = true;
                    if (!menuItem.Classes.Contains("active"))
                    {
                        menuItem.Classes.Add("active");
                    }
                    selectedPath.Push(menuItem);
                    return selectedPath;
                }

                var url = menuItem.Href.Replace(appPath, string.Empty).TrimEnd('/').ToUpperInvariant();
                 if (requestUrl == url || (!string.IsNullOrEmpty(url) && requestUrl.StartsWith(url + "/")))
                {
                    menuItem.Selected = true;
                    if (!menuItem.Classes.Contains("active")) {
                        menuItem.Classes.Add("active");
                    }
                    
                    selectedPath = new Stack<MenuItem>();
                    selectedPath.Push(menuItem);
                    return selectedPath;
                }
            }

            return null;
        }

    }
}